<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$return=array();
$return['basic']=$this->plugin_setting('basic');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['diy']=$this->plugin_setting('diy');
set_html('title','询单');
if($_RQ['op']=='main'){
	$return['params']=array('status'=>$_RQ['status']?intval($_RQ['status']):100);
	exi($return);
}
/*******op*******/
if($_RQ['op']=='list'){
	//超时未支付订单取消
	$list=pdo_getall('jy_msgorder_tcp',array('uniacid'=>$_SESSION['uniacid'],'status'=>'0'));
	if($list){
		$timeout=$return['basic']['timeout']?$return['basic']['timeout']:5;
		foreach($list as $row){
			if(time()-$row['createtime']>$timeout*60){
				pdo_update('jy_msgorder_tcp',array('status'=>4),array('id'=>$row['id']));
			}
		}
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	//查询进入该页面的客户所有订单
	$params=array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']);
	if(isset($_RQ['status']) && $_RQ['status'] != 100){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}else{
		$return['params']['status']=100;
	}
	$order_list=pdo_getslice('jy_msgorder_tcp',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	if($order_list){
		foreach($order_list as &$item){
			$worker=pdo_get('jy_msgorder_worker',array('id'=>$item['workerid'],'uniacid'=>$_SESSION['uniacid']));
			$worker['onlinetime']=json_decode($worker['onlinetime'],true);
			$worker['tags']=json_decode($worker['tags'],true);
			$worker['api_data']=json_decode($worker['api_data'],true);
			$worker['salenum']=pdo_count('jy_msgorder_tcp',array('uniacid'=>$_SESSION['uniacid'],'workerid'=>$item['workerid']));
			$item['worker']=$worker;
			$item['createtime']=date('Y-m-d H:i:s', $item['createtime']);
			$item['statustitle']=$this->tcp_status[$item['status']];
		}
	}
	$return['list']=$order_list;
	exi($return);
}
if($_RQ['op']=='receive'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	$this->model('model')->order_receive($_RQ['id']);
	exi('确认完成服务成功！');
}
if($_RQ['op']=='del'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_delete('jy_msgorder_tcp',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('删除预约成功');
}
if($_RQ['op']=='pay'){
	$post=get_postdata();
	$id=$post['id'];
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}
	$pay_id=pdo_getcolumn('jy_msgorder_tcp',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id),'pay_id');
	exi($pay_id);
}
if($_RQ['op']=='paynew'){
	$post=get_postdata();
	$id=$post['id'];
	if(!$id){
		exi('订单不存在，请重新选择订单','error');
	}
	$order=pdo_get('jy_msgorder_tcp',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$params=array(
					'id'=>$id,
					'title'=>'检测订单',
					'fee'=>$order['price'],
					'plugin'=>'jy_msgorder',
					//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
					'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
					'back_url'=>'jy_msgorder/mytcp/payresult/',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
					'success_url'=>'jy_msgorder/mytcp/',
					'error_url'=>'jy_msgorder/mytcp/',
	);
	$payid=cfc('pay')->create_pay_order($params);
	$this->pdo_update('order',array('pay_id'=>$payid),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	exi($payid);
}
if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	$order=pdo_get('jy_msgorder_tcp',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	$this->model('model')->payresult4($order,$payresult);
	exi('支付成功','success','jy_msgorder/mytcp/list/status=1');
}